package org.ling.xuan.dao;

import com.intellij.openapi.project.Project;
import org.ling.xuan.entity.mo.CommonSetting;
import org.ling.xuan.entity.mo.EmailSetting;
import org.ling.xuan.entity.mo.ProjectSetting;
import org.ling.xuan.entity.mo.Setting;

public class SettingDao {

    public static Setting getSetting(Project project, String basePath) {
        Setting setting = new Setting();
        CommonSetting commonSetting = FileDao.get(CommonSetting.class, "CommonSetting.json", new CommonSetting());
        EmailSetting emailSetting = FileDao.get(EmailSetting.class, "EmailSetting.json", new EmailSetting());
        ProjectSetting projectSetting = ProjectDao.getSetting(basePath);
        if (projectSetting == null) {
            projectSetting = ProjectSetting.initProjectSettingByProject(project);
        }
        setting.setCommonSetting(commonSetting);
        setting.setEmailSetting(emailSetting);
        setting.setProjectSetting(projectSetting);
        return setting;
    }

    public static void updateSetting(String basePath, Setting setting) {
        ProjectDao.updateSetting(basePath, setting.getProjectSetting());
        FileDao.update(setting.getCommonSetting(), "CommonSetting.json");
        FileDao.update(setting.getEmailSetting(), "EmailSetting.json");
    }
}
